package com.zues.thread.create.pool;

import com.zues.thread.util.ThreadUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * 测试线程池的invokeAll方法
 *
 * @ClassName TestInvokeAll
 * @Description TODO
 * @Author zeus
 * @Date 3/21/2025 4:24 下午
 * @Version 1.0
 **/
@Slf4j(topic = "c.TestInvokeAll")
public class TestInvokeAll {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ExecutorService pool = Executors.newFixedThreadPool(2);

        List<Future<Integer>> futures = pool.invokeAll(Arrays.asList(
                () -> {
                    log.debug("begin");
                    ThreadUtil.sleep(2);
                    return 1;
                },
                () -> {
                    log.debug("begin");
                    ThreadUtil.sleep(1);
                    return 2;
                },
                () -> {
                    log.debug("begin");
                    ThreadUtil.sleep(3);
                    return 3;
                }
        ));

        futures.forEach(future -> {
            try {
                log.debug("{}", future.get());
            } catch (Exception e) {
                e.printStackTrace();
            }
        });


        pool.shutdown();
    }

}
